﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Threading;
using ArgusLib;
using ArgusLib.BugReport;
using ArgusLib.BugReport.Protocols;
using ArgusPCLib.Bird;

namespace Imagenator
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			ServerInfo.InitializeAsync();
			ExceptionHandler.CrashDetected += new CrashDetectedHandler((e) =>
			{
				if (Forms.SendCrashReportBox.Show(e.CrashInfoFile.FullName) != DialogResult.Yes)
					return;

				//string host, adress, pw;
				//int port;
				//bool ssl;
				//MailData mailData = new MailData();
				//mailData.GetData(out host, out adress, out pw, out port, out ssl);

				//Mail mail = new Mail(
				//	host, new System.Net.NetworkCredential(adress, pw),
				//	new System.Net.Mail.MailAddress(adress),
				//	new System.Net.Mail.MailAddress(adress));
				//mail.Subject = "Bugreport Imagenator v" + ArgusLib.Environment.GetAssemblyVersion().ToString();
				//mail.Port = port;
				//mail.EnableSSL = ssl;
				//e.Protocol = mail;

				string subject = "Bugreport Imagenator v" + ArgusLib.Environment.GetAssemblyVersion().ToString();
				byte[] attachment = File.ReadAllBytes(e.CrashInfoFile.FullName);
				ServerResponses response = MailServer.Send(ServerInfo.MailServerAdress, subject, "", "", attachment, subject+".zip").Result;

				if (response != ServerResponses.SuccessResponse)
					ArgusLib.Controls.MessageBox.Show("Bugreport could not be sent.", "Error");
			});
			ExceptionHandler.Crash += new CrashHandler((e) =>
			{
				ArgusLib.Controls.ExceptionDisplayBox.Show(Localization.Text.MessageCrashText, Localization.Text.MessageCrashCaption, e.Exception.ToString());
			});
#if DEBUG
#else
			ArgusLib.BugReport.ExceptionHandler.Initialize();
#endif
			Application.Run(new MainForm());
		}
	}
}
